﻿using Gumini.JulaDB.DataTypes;

namespace Gumini.JulaDB.Expressions.Operators
{
    internal abstract class ArithmeticOperator : BinaryOperator
    {
        public ArithmeticOperator(params string[] names)
            : base(names) { }

        protected abstract decimal EvaluateCore(decimal a, decimal b);

        public override TypedValue Evaluate(TypedValue left, TypedValue right)
        {
            // result of an arithmetic operation is undefined when at least one of the operands is undefined
            if (left.IsNull || right.IsNull)
                return TypedValue.Null;

            var numberType = DataType.Number;
            decimal a = (decimal)numberType.ConvertFrom(left.Value);
            decimal b = (decimal)numberType.ConvertFrom(right.Value);

            return new TypedValue(numberType, EvaluateCore(a, b));
        }

        public override DataType GetReturnType()
        {
            return DataType.Number;
        }
    }
}
